package com.aljfce.business.profile.boundary;

import com.aljfce.business.profile.control.ProfileManager;
import com.aljfce.business.entity.User;
import com.aljfce.business.profile.control.LoginManager;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;

import javax.ejb.Stateless;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author Antonio
 */
@ManagedBean
@ViewScoped
public class GuestHome implements Serializable {
    
    private String email;
    private String password;
    
    private User user;
    //TO DO PRIVATE?
    @Inject
    private ProfileManager pm;
    @Inject
    private LoginManager lm;
    
    /**
     * Default constructor
     */
    public GuestHome (){
    }
    /**
     * This method cleans the email and password fields
     */
    @PostConstruct
    public void build(){
        this.email = "";
        this.password = "";
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getPassword() {
        return password;
    }

    public void setUser(User user) {
        this.user = user;
    }
    
    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }
    
    /**
     * This method shows the login page
     * @return {@link String} representing the login page
     */
    public String showGuestHome(){
        return "/index";
    }
    
    //TODO ANTONIO questi metodi potremmo usarli per reindirizzare le jsf chiamando i metodi e non direttamente
    public void showRegistration(){
        
    }
    
    /**
     * This method allows the users to submit the registration to the application and returns the login page
     * @return {@link String} representing the login page
     */
    public String submitRegistration(){
        Boolean userAlreadyExists = pm.newProfile(user, password);
        if(userAlreadyExists){
            FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Registration Failed this email already exists.",null));
        return null;
        }
        FacesContext context = FacesContext.getCurrentInstance();
            HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Registration Success",null));
        
        return "/index";
    }
    
    /**
     * This method allows the users to submit the login to the application and returns the home page
     * if the user's data are correct or the login page otherwise
     * @return {@link String} representing the login page or the home page
     */
    public String submitLogin(){
        Boolean logged = lm.verifyLoginData(email, password);
        if(logged){
            return "/user/index";
        }else{
            return "/index";            
        }
    }
  
    public String signUp(){
        return "/register";
    }
    //TODO ANTONIO JACOPO?
    public void checkIfLogged(){
        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
        Principal principal = request.getUserPrincipal();
        if(principal != null){
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            try {
                externalContext.redirect("./user/index.xhtml");
            } catch (IOException ex) {
                Logger.getLogger(GuestHome.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
